/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.robot.context.impl;

import java.util.HashMap;
import java.util.Map;

import org.dailydev.wave.robot.context.IBundleContext;
import org.dailydev.wave.robot.context.IEventContext;
import org.dailydev.wave.robot.context.IWaveletContext;

import com.google.wave.api.Event;
import com.google.wave.api.RobotMessageBundle;

/**
 * @author Roman Bosak, DailyDev.org
 */
public class BundleContext extends HashMapContext implements IBundleContext {

    private final RobotMessageBundle bundle;

    private final Map<Event, IEventContext> eventContexts = new HashMap<Event, IEventContext>();

    private IWaveletContext waveletContext;

    /**
     * @param contextManager
     * @param bundle
     */
    public BundleContext(RobotMessageBundle bundle) {
        super();
        this.bundle = bundle;
    }

    /** {@inheritDoc} */
    @Override
    public RobotMessageBundle getBundle() {
        return bundle;
    }

    /** {@inheritDoc} */
    @Override
    public IEventContext getEventContext(Event event) {
        IEventContext eventContext = eventContexts.get(event);
        if (eventContext == null) {
            eventContext = new EventContext(event, this);
        }
        return eventContext;
    }

    /** {@inheritDoc} */
    @Override
    public IWaveletContext getWaveletContext() {
        if (waveletContext == null) {
            waveletContext = new WaveletContext(bundle.getWavelet(), bundle.getRobotAddress());
        }
        return waveletContext;
    }

}
